//
//  BVMycolltion.m
//  BuyVegetable
//
//  Created by zhuzi on 16/11/3.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVMyCollectionViewController.h"
#import "BVLoginViewController.h"
#import "BVMyCollectionCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "BVLoginoutMyCollection.h"
#import "BVLoginViewController.h"
#import "BVEmptyMyCollection.h"
#import "BVMyCollectionTool.h"

#define CGRECT CGRectMake(0,0,ScreenW,ScreenH-64)
#define SHOPPINGCART_CELL_IDENTIFIER @"BVMyCollectionCell"

@interface BVMyCollectionViewController ()<UITableViewDelegate,UITableViewDataSource,myCollectionCell>
{
    UILabel *_GoodsCountLabel;
    UIButton *_payButton;
}
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIButton *selectAllButton;
@property (strong, nonatomic) UIButton *shoppingButton;

@property (nonatomic, strong) BVLoginoutMyCollection *logoutView;
@property (nonatomic, strong) BVEmptyMyCollection *emptyView;

#pragma mark - 正式
@property (nonatomic, strong) BVMyCollectionTool *myCollectionTool;
@property (nonatomic, strong) NSMutableArray *dataArr2;
@property (nonatomic, strong) NSMutableArray *selectedArr2;

@end

@implementation BVMyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self chooseChildView];
    [self setUI];
    [self setFootView];
    [self fetchMyCollectionData];
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64-48) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor BVBackgroundLightGrayColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[BVMyCollectionCell class] forCellReuseIdentifier:SHOPPINGCART_CELL_IDENTIFIER];
    }
    return _tableView;
}
- (void)setUI{
    
    self.navigationItem.title = @"我的关注";
    self.view.backgroundColor = [UIColor BVBackgroundLightGrayColor];
    [self.view addSubview:self.tableView];
}
- (void)chooseChildView {
    
    if (!self.user.login) {
        self.logoutView = [[BVLoginoutMyCollection alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
        [self.view addSubview:self.logoutView];
        __weak __typeof(self) weakSelf = self;
        self.logoutView.logoutBlock = ^(){
            BVLoginViewController * login = [[BVLoginViewController alloc]init];
            [weakSelf.navigationController pushViewController:login animated:YES];
        };
        
    }else{
        [self.logoutView removeFromSuperview];
        if (self.viewType == 0) {
            self.emptyView = [[BVEmptyMyCollection alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
            [self.view addSubview:self.emptyView];
            __weak __typeof(self) weakSelf = self;
            self.emptyView.goCollectionBlock = ^() {
                weakSelf.tabBarController.selectedIndex = 1;
            };
        }else{
            [self.emptyView removeFromSuperview];
        }
    }
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //直接计算
     return [self cellHeightForIndexPath:indexPath cellContentViewWidth:ScreenW tableView:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr2.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BVMyCollectionCell *myCollection_cell = [tableView dequeueReusableCellWithIdentifier:SHOPPINGCART_CELL_IDENTIFIER];
    
    BVMycollectionModel *mycollectionModel = self.dataArr2[indexPath.row];
    myCollection_cell.mycollectionModel = mycollectionModel;
    myCollection_cell.radioButton.selected = mycollectionModel.is_selected;
    myCollection_cell.indexPath = indexPath;
    myCollection_cell.delegate = self;
#pragma mark -
#pragma mark - 操作相关
    myCollection_cell.radioBlock = ^(NSIndexPath *indexPath,BOOL is_selected){
        BVMycollectionModel *mycollectionModel = self.dataArr2[indexPath.row];
        mycollectionModel.is_selected = is_selected;
        
        /* 单选按钮把失效的择出来 */
        NSInteger valid_count = 0; NSInteger disvalid_count = self.dataArr2.count;
        for (BVMycollectionModel *mycollectionModel in self.dataArr2) {
            
            if ([mycollectionModel.status isEqualToString:@"1"]) {
                disvalid_count--;
                if (mycollectionModel.is_selected) {
                    valid_count++;
                }
            }
        }
        if (valid_count == self.dataArr2.count - disvalid_count) {
            self.selectAllButton.selected = YES;
        } else {
            self.selectAllButton.selected = NO;
        }
        [self.tableView reloadData];
        
#pragma mrk - 共 多少件商品
        NSMutableArray *shoppingcartArr = [[NSMutableArray alloc] init];
        for (BVMycollectionModel *mycollectionModel in self.dataArr2) {
            if (mycollectionModel.is_selected && [mycollectionModel.status isEqualToString:@"1"]) {
                NSLog(@"==>%@",mycollectionModel.ID2);
                [shoppingcartArr addObject:mycollectionModel];
            }
        }
        _GoodsCountLabel.text = [NSString stringWithFormat:@"共 %lu 件商品",(unsigned long)shoppingcartArr.count];
    };
    return myCollection_cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark -
#pragma mark - 加入购物车
- (IBAction)shoppingClick:(id)sender {
    NSMutableArray *shoppingcartArr = [[NSMutableArray alloc] init];
    for (BVMycollectionModel *mycollectionModel in self.dataArr2) {
        if (mycollectionModel.is_selected && [mycollectionModel.status isEqualToString:@"1"]) {
            NSLog(@"==>%@",mycollectionModel.ID2);
            [shoppingcartArr addObject:mycollectionModel];
        }
    }
    NSLog(@"==>%ld",shoppingcartArr.count);
    if (shoppingcartArr.count == 0) {
        return;
    }
    
    [self.myCollectionTool addShoppingCartGoods_idArr:shoppingcartArr success:^(NSMutableDictionary *arr) {
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark -
#pragma mark - 全选
- (IBAction)selectAllClick:(UIButton *)button {
    button.selected = !button.selected;
    for (BVMycollectionModel *mycollectionModel in self.dataArr2) {
        mycollectionModel.is_selected = button.selected;
    }
    [self.tableView reloadData];
    
//  共 多少件商品
    NSMutableArray *shoppingcartArr = [[NSMutableArray alloc] init];
    for (BVMycollectionModel *mycollectionModel in self.dataArr2) {
        if (mycollectionModel.is_selected && [mycollectionModel.status isEqualToString:@"1"]) {
            NSLog(@"==>%@",mycollectionModel.ID2);
            [shoppingcartArr addObject:mycollectionModel];
        }
    }
    _GoodsCountLabel.text = [NSString stringWithFormat:@"共 %lu 件商品",(unsigned long)shoppingcartArr.count];
}
#pragma mark - 设置 Arr2 - 正式
- (void)setDataArr2:(NSMutableArray *)dataArr2{
    _dataArr2 = dataArr2;

}
#pragma mark - 我的收藏 工具类类 - 正式
- (BVMyCollectionTool *)myCollectionTool{
    if (!_myCollectionTool) {
        _myCollectionTool = [[BVMyCollectionTool alloc]init];
    }
    return _myCollectionTool;
}
#pragma mark - 获取我的收藏数据 - 正式
- (void)fetchMyCollectionData{
    [self.hud show:YES];
    [self.myCollectionTool fetchmyCollectionSuccess:^(NSMutableArray *arr) {
        self.dataArr2 = (NSMutableArray *)arr;
        [self.tableView reloadData];
        [self.hud hide:YES];
        if (self.dataArr2.count == 0) {
            self.viewType = 0;
            [self chooseChildView];
        }else{
            self.viewType = 1;
            [self chooseChildView];
        }
    } failure:^(NSError *error) {
        [self.hud hide:YES];
    }];
}
#pragma mark - cell 上的删除按钮 传过来的代理
- (void)delegateButClick:(NSIndexPath *)indexPath{
    [self.hud show:YES];
    BVMycollectionModel *mycollectionModel = self.dataArr2[indexPath.row];
    [self.myCollectionTool delegateCollectionGoods_ID:mycollectionModel.ID2 success:^(NSMutableDictionary *dic) {
        [self.hud hide:YES];
        if ([dic[@"code"] isEqualToString:@"0"]) {
            [self showSuccess:@"取消成功"];
        }else{
            [self showError:@"取消失败"];
        }
    } failure:^(NSError *error) {
            [self.hud hide:YES];
            [self showError:@"未知错误"];
    }];
    [self fetchMyCollectionData];
}
#pragma mark - setFootView
- (void)setFootView {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0,ScreenH - 64 - 48 , ScreenW, 48)];
    [self.view addSubview:footView];
    footView.backgroundColor = [UIColor whiteColor];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 0.5)];
    line.backgroundColor = [UIColor BVBackgroundLightGrayColor];
    [footView addSubview:line];
    
    _selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [footView addSubview:_selectAllButton];
    [_selectAllButton setImage:[UIImage imageNamed:@"huiduihao"] forState:UIControlStateNormal];
    [_selectAllButton setImage:[UIImage imageNamed:@"hongduihao"] forState:UIControlStateSelected];
    _selectAllButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [_selectAllButton addTarget:self action:@selector(selectAllClick:) forControlEvents:UIControlEventTouchUpInside];
    _selectAllButton.sd_layout
    .topSpaceToView(footView,0)
    .bottomSpaceToView(footView,0)
    .leftSpaceToView(footView,0)
    .widthEqualToHeight();
    
    UILabel *allselectLabel = [[UILabel alloc] init];
    [footView addSubview:allselectLabel];
    allselectLabel.text = @"全选";
    allselectLabel.sd_layout
    .topSpaceToView(footView,0)
    .bottomSpaceToView(footView,0)
    .leftSpaceToView(_selectAllButton,0)
    .widthIs(35);
    
    _payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [footView addSubview:_payButton];
    _payButton.backgroundColor = [UIColor BVBaseOrangeColor];
    [_payButton setTitle:@"加入购物车(0)" forState:0];
    [_payButton setTitleColor:[UIColor whiteColor] forState:0];
    [_payButton addTarget:self action:@selector(shoppingClick:) forControlEvents:UIControlEventTouchUpInside];
    _payButton.titleLabel.font = [UIFont systemFontOfSize:16];
    _payButton.sd_layout
    .topEqualToView(footView)
    .bottomEqualToView(footView)
    .rightEqualToView(footView)
    .widthIs(120);
    
    _GoodsCountLabel = [[UILabel alloc] init];
    [footView addSubview:_GoodsCountLabel];
    _GoodsCountLabel.font = [UIFont systemFontOfSize:14];
    _GoodsCountLabel.textColor  = [UIColor blackColor];
    _GoodsCountLabel.textAlignment = NSTextAlignmentLeft;
    _GoodsCountLabel.text = [NSString stringWithFormat:@"共 0 件商品"];
    _GoodsCountLabel.sd_layout
    .topSpaceToView(footView,0)
    .bottomSpaceToView(footView,0)
    .rightSpaceToView(_payButton,0)
    .leftSpaceToView(allselectLabel,20);
    
}
@end
